package com.mms.controller;


import com.alibaba.fastjson.JSONObject;
import com.mms.enumerate.CodeEnum;
import com.mms.pojo.Resources;
import com.mms.pojo.UserInfo;
import com.mms.service.IResourcesService;
import com.mms.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    /**
     * 用户信息服务
     */
    @Autowired
    IUserInfoService userInfoService;

    /**
     * 资源服务
     */
    @Autowired
    IResourcesService resourcesService;

    @RequestMapping(value = "/")
    public String index() {
        return "redirect:/login.jsp";
    }

//    /**
//     * 病患登录
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "login.a")
//    public String TestLogin(HttpServletRequest request) {
//
//        UserInfo userInfo = userInfoService.verifyUser("BH2"
//                , "123456");
//
//        request.getSession().setAttribute("userinfo", userInfo);
//        JSONObject retJson = new JSONObject();
//        retJson.put("code", "0");
//        retJson.put("msg", "登录成功");
//        return retJson.toString();
//    }
//
//    /**
//     * 医生 登录
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "login.b")
//    public String testLogin(HttpServletRequest request) {
//        UserInfo userInfo = userInfoService.verifyUser("user1", "1");
//        if (userInfo == null) {
//            return CodeEnum.SignInFail.toString();
//        }
//        request.getSession().setAttribute("userinfo", userInfo);
//
//        return CodeEnum.okSignIn.toString();
//    }
//
//    /**
//     * 管理员登录
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "login.c")
//    public String testLogin2(HttpServletRequest request) {
//        UserInfo userInfo = userInfoService.verifyUser("admin", "1");
//        request.getSession().setAttribute("userinfo", userInfo);
//
//        return CodeEnum.okSignIn.toString();
//    }

    /**
     * 错误界面
     *
     * @return
     */
    @RequestMapping(value = "errorJsp")
    public String errorJsp() {
        return "404";
    }

    /**
     * 用户操作界面
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView indexJsp(HttpServletRequest request) {
        //创建图新对象
        ModelAndView mv = new ModelAndView();
        //获取 用户信息
        UserInfo userinfo = (UserInfo) request.getSession().getAttribute("userinfo");
        //获取 用户 资源
        List<Resources> resources = resourcesService.getResourcesByUserinfo(userinfo.getId());
        //分析资源
        Map<Resources, List<Resources>> listMapResources = resourcesService.parseHierarchy(resources);
        //加入资源对象
        mv.addObject("listMapResources", listMapResources);
        //设置jsp界面
        mv.setViewName("primary/index");
        return mv;
    }
}
